package com.frskycockpit.fragment;

import com.frskycockpit.R;
import com.frskycockpit.object.Sensor;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

/**
 * Sensor fragment
 *
 * @author jean
 *
 */
public class SensorFragment extends Fragment implements OnClickListener {
    public static final String ID_KEY = "com.frskycockpit.SensorFragment.ID_KEY";
    public static final String PREFS_NAME = "SensorFragmentSettings";
    private int mId;
    private Sensor mSensor = null;
    private TextView tv_sensor_value = null;
    private Button bt_settings = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mId = getArguments().getInt(ID_KEY);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // affichage
        View view;
        view = inflater
                .inflate(R.layout.fragment_sensor, container, false);
        TextView tv_cap1name = (TextView) view.findViewById(R.id.tv_cap1name);
         tv_sensor_value = (TextView) view.findViewById(R.id.tv_sensor_value);

        bt_settings = (Button) view.findViewById(R.id.bt_settings);
        bt_settings.setOnClickListener(this);
        // on charge la configuration du Sensor
        int id = getSettings();
        if (id != -1) {
            bt_settings.setText(getResources().getStringArray(
                    R.array.SensorName)[id]);
            if (getResources().getStringArray(
                    R.array.SensorName)[id].equals("A1")) {
                mSensor = Sensor.loadSensor(getActivity(), Sensor.SENSOR_TYPE_A1);
            } else if (getResources().getStringArray(
                    R.array.SensorName)[id].equals("A2")) {
                mSensor = Sensor.loadSensor(getActivity(), Sensor.SENSOR_TYPE_A2);
            }
            if (mSensor != null) {
                tv_cap1name.setText(mSensor.getLabel());
            }
        }

        return view;
    }

    public static SensorFragment newInstance(int id) {
        SensorFragment f = new SensorFragment();
        Bundle args = new Bundle();
        args.putInt(ID_KEY, id);
        f.setArguments(args);
        return f;
    }

    /**
     * refresh value on fragment
     */
    public void refreshValue(int value) {
        if (mSensor != null)
            tv_sensor_value.setText(mSensor.getValue(value));
    }

    public void showSpinner(boolean show) {
        if (show)
            bt_settings.setVisibility(View.VISIBLE);
        else
            bt_settings.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        if (v == bt_settings) {
            int id = getSettings();
            if (id == -1)
                id = 0;
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle(getString(R.string.ChooseSensor));
            builder.setSingleChoiceItems(R.array.SensorName, id,
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            bt_settings.setText(getResources().getStringArray(
                                    R.array.SensorName)[which]);
                            saveSettings(which);
                            dialog.dismiss();
                        }
                    });
            AlertDialog alert = builder.create();
            alert.show();
        }
    }

    private void saveSettings(int id) {
        SharedPreferences settings = getActivity().getSharedPreferences(
                PREFS_NAME, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt("SensorId" + mId, id);
        editor.commit();
    }

    private int getSettings() {
        SharedPreferences settings = getActivity().getSharedPreferences(
                PREFS_NAME, 0);
        return settings.getInt("SensorId" + mId, -1);
    }

    public Sensor getSensor() {
        return mSensor;
    }
}
